package com.drools.rule.execute;

import com.alibaba.fastjson.JSON;
import com.drools.rule.req.ActionReq;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RuleExecute {
    public ActionReq execute(List<String> rulesContext, ActionReq actionReq){
        System.setProperty("drools.dateformat", "yyyy-MM-dd HH:mm:ss");
        KieHelper helper = new KieHelper();
        for (String ruleContext : rulesContext) {
            helper.addContent(ruleContext, ResourceType.DRL);
        }
        ArrayList<String> result = new ArrayList();
        KieSession kSession = helper.build().newKieSession();
        kSession.setGlobal("result", result);
        kSession.insert(actionReq);
        kSession.fireAllRules();
        kSession.dispose();
        System.out.println(JSON.toJSONString(result));
        return actionReq;
    }
}
